package simorg.actions;

import java.util.Date;

import simorg.controller.AssignmentController;
import simorg.domain.Assignment;
import simorg.domain.persistence.AssignmentTable;
import simorg.view.ModelView;
import simorg.view.WebSessionContext;

public class AssignmentNew extends Action {

	public static String actionName = "new";
	public AssignmentNew() {
		super(actionName);
		this.controllerName = AssignmentController.controllerName;
	}
	public AssignmentNew(String assignmentId) {
		super(actionName);
		this.controllerName = AssignmentController.controllerName;
		this.getRequestArgs().add(assignmentId);
	}
	
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}
	
	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		// Create a new assignment 
		int courseId = Integer.parseInt(wc.getRequestParameter("courseId"));
		Assignment a = new Assignment();
		a.setTitle("New Assignment");
		a.setDueDate(new Date());
		a.setAssignedDate(new Date());
		a.setNumCompoundsToComplete(10);
		a.setCourseId(courseId);
		AssignmentTable.save(wc.getDbConnection(), a);
		Action redirect = new AssignmentView(String.valueOf(a.getId()));
		wc.redirectAdmin(redirect);
		return null;
	}

}
